package com.jxchexie.signin;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.utils.TimeUtils;
import com.jxchexie.api.Api;
import com.jxchexie.app.App;
import com.jxchexie.model.MyMessage;
import com.jxchexie.utils.JsonUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class MessageActivity extends AppCompatActivity {
    TextView mDate;
    TextView mTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        initView();
        getData();
    }

    private void initView() {
        mDate = (TextView)findViewById(R.id.mDate);
        mTitle = (TextView)findViewById(R.id.mTitle);
    }


    private void getData() {
        java.text.SimpleDateFormat ss = new java.text.SimpleDateFormat("yyyy年-MM月-dd日");
        String date = TimeUtils.getCurTimeString(ss);
        /*主要是为了拼凑unique_id*/
        java.text.SimpleDateFormat ss2 = new java.text.SimpleDateFormat("yyyyMMdd");
        String date2= TimeUtils.getCurTimeString(ss2);
        /*获得当前登录用户的用户名*/
        String user_name=App.getInstance().getUser(MessageActivity.this);
        /*没人每天只有一个id*/
        String unique_id=user_name+date2;
        HashMap<String, String> params = new HashMap<>();
        params.put("date",date);
        params.put("unique_id",unique_id);
        params.put("user_name",user_name);
        JSONObject jsonObject = new JSONObject(params);
        OkGo.post(Api.GET_MESSAGE)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        MyMessage message = new MyMessage();
                        message = JsonUtils.fromJson(s,MyMessage.class);
                        if(message.getStatus() == "error"){
                            Toast.makeText(MessageActivity.this,message.getMsg(),Toast.LENGTH_SHORT).show();
                        }else if (message.getHaveInformationToSent().equals("1")){
                        mTitle.setText(message.getMsg());
                        mDate.setText(message.getDate());
                        Toast.makeText(MessageActivity.this,message.getMsg(),Toast.LENGTH_SHORT).show();
                    }else if (message.getHaveInformationToSent().equals("0")){
                            Toast.makeText(MessageActivity.this,"今日已签到完成!",Toast.LENGTH_SHORT).show();
                        }
                }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        MaterialDialog dialog = new MaterialDialog.Builder(MessageActivity.this)
                                .title("提示")
                                .content("未连接到服务器请重试！")
                                .positiveText("确定")
                                .show();
                    }
                });

    }



}